package com.hubu.web;

import com.hubu.mapper.studentUserMapper;
import com.hubu.pojo.grade_Information;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

@WebServlet("/insertCourse")
public class insertCourse extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        PrintWriter writer = response.getWriter();
        //获取学生数据

        StringBuffer student =new StringBuffer("");
        Cookie[] cookies=request.getCookies();

        if(cookies!=null){
        for(Cookie cookie : cookies)
        {
            String name=cookie.getName();
            if("username".equals(name)){
                student.append(cookie.getValue());
            }
        }}

        String id=new String(student);  //利用Cookie在找到登录的学生id

        String course_id=request.getParameter("course_id");

        String teacher_id=request.getParameter("teacher_id");
        String credits=request.getParameter("credits");

        System.out.println(id);
        System.out.println("1");
        System.out.println(teacher_id);
        System.out.println(course_id);
        System.out.println(credits);
        //封装学生选课数据
        grade_Information course =new grade_Information();
        course.setId(id);
        course.setCourse_id(course_id);
        course.setTerm("1");
        course.setTeacher_id(teacher_id);
        //course.setGrade();
        course.setCredits(Float.parseFloat(credits));

        //2. 调用Mybatis完成查询
        // 2.1 获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);

        //2.2获取SqlSession对象
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //2.3 获取Mapper
        studentUserMapper userMapper=sqlSession.getMapper(studentUserMapper.class);


        grade_Information existingCourse = userMapper.selectByCourse(course_id, id);
        if(existingCourse==null) {

            userMapper.inChooseCourse(course);
            //提交数据库
            sqlSession.commit();
            //释放资源
            writer.println("<script type='text/javascript'>");
            writer.println("alert('选课成功！');");
            writer.println("window.location='" + request.getContextPath() + "/enterCourseSelection';");
            writer.println("</script>");
            sqlSession.close();
        }
        else{
            //该课程已经
            writer.println("<script type='text/javascript'>");
            writer.println("alert('选课失败，课程已经被选！');");
            writer.println("window.location='" + request.getContextPath() + "/enterCourseSelection';");
            writer.println("</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}